import numpy as np
import csv
import random
from datetime import datetime
from time import strftime
from dbfpy import dbf

'''
read dbf file
'''

def getCurTime():
    """
    get current time
    Return value of the date string format(%Y-%m-%d %H:%M:%S)
    """
    format='%Y-%m-%d %H:%M:%S'
    sdate = None
    cdate = datetime.now()
    try:
        sdate = cdate.strftime(format)
    except:
        raise ValueError
    return sdate

def build_data_list(inputCSV):
    sKey = []
    fn = inputCSV
    ra = csv.DictReader(file(fn), dialect="excel")
    
    for record in ra:
        #print record[ra.fieldnames[0]], type(record[ra.fieldnames[-1]])
        for item in ra.fieldnames:
            temp = float(record[item])
            sKey.append(temp)
    sKey = np.array(sKey)
    sKey.shape=(-1,len(ra.fieldnames))
    return sKey


def build_data_dbf(inputDBF):
    sKey = np.array([])
    fn = inputDBF
    db = dbf.Dbf(fn)
    for record in db:
        temp = float(record[db.fieldNames[2]])
        if temp < 0.05:
            temp_id = int(float(record[db.fieldNames[0]]))
            sKey = np.append(sKey, temp_id)
            temp_id = int(float(record[db.fieldNames[1]]))
            sKey = np.append(sKey, temp_id)
            sKey = np.append(sKey, temp)
    sKey.shape = (-1, 3)
    return sKey


#--------------------------------------------------------------------------
#MAIN
if __name__ == "__main__":
    print "begin at " + getCurTime()
    filePath = 'C:/_DATA/migration89_08/COUNTY Migration/csv/'
    inputCSV = filePath + 'total_in.csv'
    print inputCSV
    #print np.__version__
    data = build_data(inputCSV)
    #output = []
    #for item in data:
        #data.append(generate_fips(item[1], item[2]))
        #data.append(generate_fips(item[3], item[4]))

    #output = np.array(output)
    #output.shape = (-1, 13)

    fileLoc = filePath + 'total_in_FIPS.csv'
    np.savetxt(fileLoc, data, delimiter=',', fmt = '%s')

    print "end at " + getCurTime()
    print "========================================================================"  
    
            